class CulturalHeritage::CareAndMaintenancesController < ApplicationController
  before_filter :authenticate_user!
  # GET /cultural_heritage/care_and_maintenances
  # GET /cultural_heritage/care_and_maintenances.xml
  def index
    @title_view = 'Cuidado y Mantenimiento'
    
    @cultural_heritage_care_and_maintenances = CulturalHeritage::CareAndMaintenance.all

    @lista_heritage_select=CulturalHeritage::CulturalHeritage.all
    @lista_institution_select=CulturalHeritage::CulturalInstitution.all
    @security_person=Security::Person.all
    @care_and_maintenance_supervisors=Security::Person.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @cultural_heritage_care_and_maintenances }
    end
  end

  # GET /cultural_heritage/care_and_maintenances/1
  # GET /cultural_heritage/care_and_maintenances/1.xml
  def show
    @title_view = 'Cuidado y Mantenimiento'
    @cultural_heritage_care_and_maintenance = CulturalHeritage::CareAndMaintenance.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @cultural_heritage_care_and_maintenance }
    end
  end

  # GET /cultural_heritage/care_and_maintenances/new
  # GET /cultural_heritage/care_and_maintenances/new.xml
  def new
    @cultural_heritage_care_and_maintenance = CulturalHeritage::CareAndMaintenance.new
    @title_view = 'Cuidado y Mantenimiento'
    @cultural_heritages = CulturalHeritage::CulturalHeritage.all
    @cultural_institutions = CulturalHeritage::CulturalInstitution.all
    @cultural_care_and_maintenance_types = CulturalHeritage::CareAndMaintenanceType.all
    @security_people = Security::Person.all
    @care_and_maintenance_supervisors=Security::Person.all

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @cultural_heritage_care_and_maintenance }
    end
  end

  # GET /cultural_heritage/care_and_maintenances/1/edit
  def edit
    @title_view = 'Cuidado y Mantenimiento'
    @cultural_heritage_care_and_maintenance = CulturalHeritage::CareAndMaintenance.find(params[:id])
    @cultural_heritages = CulturalHeritage::CulturalHeritage.all
    @cultural_institutions = CulturalHeritage::CulturalInstitution.all
    @cultural_care_and_maintenance_types = CulturalHeritage::CareAndMaintenanceType.all
    @security_people=Security::Person.all
    @care_and_maintenance_supervisors=Security::Person.all

    @lalala=@cultural_heritage_care_and_maintenance.mayor

#    @care_and_maintenance_mayors = Array.new() { [{id=>0, name=>"Menor"},{id=>1, name=>"Mayor"}]  }
  end

  # POST /cultural_heritage/care_and_maintenances
  # POST /cultural_heritage/care_and_maintenances.xml
  def create
    @title_view = 'Cuidado y Mantenimiento'

    @cultural_heritage_care_and_maintenance = CulturalHeritage::CareAndMaintenance.new(params[:cultural_heritage_care_and_maintenance])

    @cultural_heritages = CulturalHeritage::CulturalHeritage.all
    @cultural_institutions = CulturalHeritage::CulturalInstitution.all
    @cultural_care_and_maintenance_types = CulturalHeritage::CareAndMaintenanceType.all
    @security_people=Security::Person.all
    @care_and_maintenance_supervisors=Security::Person.all
    
    respond_to do |format|
      if @cultural_heritage_care_and_maintenance.save
        format.html { redirect_to(@cultural_heritage_care_and_maintenance, :notice => 'Care and maintenance was successfully created.') }
        format.xml  { render :xml => @cultural_heritage_care_and_maintenance, :status => :created, :location => @cultural_heritage_care_and_maintenance }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @cultural_heritage_care_and_maintenance.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /cultural_heritage/care_and_maintenances/1
  # PUT /cultural_heritage/care_and_maintenances/1.xml
  def update
    @title_view = 'Cuidado y Mantenimiento'
    @cultural_heritage_care_and_maintenance = CulturalHeritage::CareAndMaintenance.find(params[:id])
    @cultural_heritages = CulturalHeritage::CulturalHeritage.all
    @cultural_institutions = CulturalHeritage::CulturalInstitution.all
    @cultural_care_and_maintenance_types = CulturalHeritage::CareAndMaintenanceType.all
    @security_person=Security::Person.all
    @care_and_maintenance_supervisors=Security::Person.all
    respond_to do |format|
      if @cultural_heritage_care_and_maintenance.update_attributes(params[:cultural_heritage_care_and_maintenance])
        format.html { redirect_to(@cultural_heritage_care_and_maintenance, :notice => 'Care and maintenance was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @cultural_heritage_care_and_maintenance.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /cultural_heritage/care_and_maintenances/1
  # DELETE /cultural_heritage/care_and_maintenances/1.xml
  def destroy
    @cultural_heritage_care_and_maintenance = CulturalHeritage::CareAndMaintenance.find(params[:id])
    @cultural_heritage_care_and_maintenance.destroy

    respond_to do |format|
      format.html { redirect_to(cultural_heritage_care_and_maintenances_url) }
      format.xml  { head :ok }
    end
  end



  def update_table
    @search_text = params[:search]
#    @search_combo = params[:search_complaint_type]

       #Instituciones culturales :: OJO, eliminar cultural description cuando la carga sea muy alta

    @care_and_maintenance_types =
        CulturalHeritage::CareAndMaintenanceType.find(
          :all,
          :conditions=>["cultural_heritage_care_and_maintenance_types.name LIKE ? ", "%"+@search_text.to_s+"%"]
        );

    @cultural_institutions =
        CulturalHeritage::CulturalInstitution.find(
          :all,
          :conditions=>["cultural_heritage_cultural_institutions.name LIKE ? or cultural_heritage_cultural_institutions.description LIKE ?", "%"+@search_text.to_s+"%", "%"+@search_text.to_s+"%"]
        );

    @care_and_maintenance_cultural_heritages =
        CulturalHeritage::CulturalHeritage.find(
          :all,
          :conditions=>["cultural_heritage_cultural_heritages.nationalRegistrationCode LIKE ? OR cultural_heritage_cultural_heritages.name LIKE ? OR cultural_heritage_cultural_heritages.description LIKE ?", "%"+@search_text.to_s+"%", "%"+@search_text.to_s+"%", "%"+@search_text.to_s+"%"]
        );

    #Ahora busco dicho texto en los campos ed tipo string de Collections
    if ! @search_text.blank?
#      if ! @search_combo.blank?
#        @cultural_heritage_collections = CulturalHeritage::Collection.find(:all,:conditions=>["cultural_heritage_collections.id = ? and cultural_heritage_collections.complaint_type = ? ",@search_text,@search_combo])
#      else
          @cultural_heritage_care_and_maintenances =
            CulturalHeritage::CareAndMaintenance.find(
              :all,
              :conditions=>
                [ "cultural_heritage_care_and_maintenances.id = ? OR " +
                  "cultural_heritage_care_and_maintenances.description LIKE ? OR " +
                  "cultural_heritage_care_and_maintenances.cultural_institution in (?) OR " +
                  "cultural_heritage_care_and_maintenances.care_and_maintenance_type in (?) OR " +
                  "cultural_heritage_care_and_maintenances.cultural_heritage in (?)",
                  @search_text,
                  "%"+@search_text.to_s+"%",
                  (@cultural_institutions.empty? ? '' : @cultural_institutions.map(&:id)),
                  (@care_and_maintenance_types.empty? ? '' : @care_and_maintenance_types.map(&:id)),
                  (@care_and_maintenance_cultural_heritages.empty? ? '' : @care_and_maintenance_cultural_heritages.map(&:id))
                ]
              )
#      end
    else
#      if !@search_combo.blank?
#        @cultural_heritage_collections = CulturalHeritage::Collection.find(:all,:conditions=>["cultural_heritage_collections.complaint_type = ? ",@search_combo])
#      else
        @cultural_heritage_care_and_maintenances = CulturalHeritage::CareAndMaintenance.all
#      end
    end
    render :partial => "table"
  end

  def is_mayor
    @care = CulturalHeritage::CareAndMaintenance.find(params[:id])
    @care.mayor=1
    @care.save
    respond_to do |format|
      format.html { redirect_to(cultural_heritage_care_and_maintenances_url) }
      format.xml  { head :ok }
    end

#    IncidenceMailer.attend_email(@incidence).deliver
  end

  def proceed
    @care = CulturalHeritage::CareAndMaintenance.find(params[:id])
    @care.status=1
    @care.save
    respond_to do |format|
      format.html { redirect_to(cultural_heritage_care_and_maintenances_url) }
      format.xml  { head :ok }
    end

#    IncidenceMailer.attend_email(@incidence).deliver
  end

  def notapplicable
    @care = CulturalHeritage::CareAndMaintenance.find(params[:id])
    @care.status=2
    @care.save
    respond_to do |format|
      format.html { redirect_to(cultural_heritage_care_and_maintenances_url) }
      format.xml  { head :ok }
    end

  end
end
